import { areaConfig } from '@/config/city-config.js'
const { theme, roadType, areaCode } = require('@/config')

/**
 * 页面配置 - 大屏母版页面相关配置
 * @author 朱琦
 * @date   时间：2022/10/10
 */

const defaultState = () => {
  return {
    // 主题
    theme,
    // 路网类型
    roadType: null,
    // 地区编码
    areaCode: null,
    // 是否是省级
    isProvince: true,
    // 中心点位
    centerPath: null,
    // 详情(三级菜单)相关配置
    detailConfig: {
      type: null, // 触发模块
      actions: null, // 触发动作
      data: {} // 相关数据
    },
    // 当前页面数据
    pageData: {},
    // 当前页面配置
    pageConfig: null,
    // 当前子页面配置
    pageChildConfig: null
  }
}
const state = defaultState()

const getters = {
  theme: state => state.theme,
  roadType: state => state.roadType,
  isProvince: state => state.isProvince,
  areaCode: state => state.areaCode,
  detailConfig: state => state.detailConfig,
  pageData: state => state.pageData,
  pageConfig: state => state.pageConfig,
  pageChildConfig: state => state.pageChildConfig
}

const mutations = {
  /**
   * 设置主题
   * @param {*} state
   * @param {*} theme
   */
  SET_THEME(state, theme) {
    state.theme = theme
  },
  /**
   * 设置路网类型
   * @param {*} state
   * @param {*} roadType
   */
  SET_ROAD_TYPE(state, roadType) {
    state.roadType = roadType
  },
  /**
   * 设置地区编码
   * @param {*} state
   * @param {*} areaCode
   */
  SET_AREA_CODE(state, areaCode) {
    state.areaCode = areaCode
    state.isProvince = areaCode.length === 2 || String(areaCode).includes('0000')
    const index = areaConfig.findIndex(i => i.key === areaCode)
    state.centerPath = index > -1 ? areaConfig[index].centerPath : null
  },
  /**
   * 设置当前详情配置数据
   * @param {*} state
   * @param {*} param1
   */
  SET_DETAIL_CONFIG(state, { type = null, actions = 'click', data = null, config = null }) {
    state.detailConfig = {
      type,
      actions,
      data,
      config
    }
  },
  /**
   * 设置当前页面数据
   * @param {*} state
   * @param {*} pageData
   */
  SET_PAGE_DATA(state, { pageTitle, logoPath }) {
    state.pageData = {
      title: pageTitle, // 标题
      logo: logoPath // logo
    }
  },
  /**
   * 设置当前页面配置
   * @param {*} state
   * @param {*} pageConfig
   */
  SET_PAGE_CONFIG(state, pageConfig) {
    state.pageConfig = pageConfig
  },

  /**
   * 设置当前子页面配置
   * @param {*} state
   * @param {*} pageChildConfig
   */
  SET_PAGE_CHILD_CONFIG(state, pageChildConfig) {
    state.pageChildConfig = pageChildConfig
  }
}
const actions = {}
export default { state, getters, mutations, actions }
